package com.ruoyi.web.controller.work;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.integrate.service.DBService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.work.domain.WorkProject;
import com.ruoyi.work.service.IWorkProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

/**
 * @Author: MMDZ
 * @Desc: TODO 项目配置
 */
@RestController
@RequestMapping("/work/project")
public class WorkProjectController extends BaseController {

    @Autowired
    private IWorkProjectService workProjectService;
    @Autowired
    private DBService dbService;

    /**
     * 查询项目配置列表
     */
    @PreAuthorize("@ss.hasPermi('work:project:list')")
    @GetMapping("/list")
    public TableDataInfo list(WorkProject workProject) {
        startPage();
        List<WorkProject> list = workProjectService.selectWorkProjectList(workProject);
        return getDataTable(list);
    }

    /**
     * 同步U8数据
     */
    @GetMapping("/sync")
    public AjaxResult sync() {
        List<String> list = dbService.syncU8Data();
        return Objects.nonNull(list) ? AjaxResult.success(list) : AjaxResult.error("同步U8数据失败");
    }

    /**
     * 导出项目配置列表
     */
    @PreAuthorize("@ss.hasPermi('work:project:export')")
    @Log(title = "项目配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WorkProject workProject) {
        List<WorkProject> list = workProjectService.selectWorkProjectList(workProject);
        ExcelUtil<WorkProject> util = new ExcelUtil<WorkProject>(WorkProject.class);
        util.exportExcel(response, list, "项目配置数据");
    }

    /**
     * 获取项目配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('work:project:query')")
    @GetMapping(value = "/{projectId}")
    public AjaxResult getInfo(@PathVariable("projectId") Long projectId) {
        return AjaxResult.success(workProjectService.selectWorkProjectById(projectId));
    }

    /**
     * 新增项目配置
     */
    @PreAuthorize("@ss.hasPermi('work:project:add')")
    @Log(title = "项目配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody WorkProject workProject) {
        if (UserConstants.NOT_UNIQUE.equals(workProjectService.checkProjectCodeUnique(workProject))) {
            return AjaxResult.error("新增项目配置'" + workProject.getProjectCode() + "'失败，项目代码已存在");
        }
        workProject.setCreateBy(getUsername());
        return toAjax(workProjectService.insertWorkProject(workProject));
    }

    /**
     * 修改项目配置
     */
    @PreAuthorize("@ss.hasPermi('work:project:edit')")
    @Log(title = "项目配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WorkProject workProject) {
        if (UserConstants.NOT_UNIQUE.equals(workProjectService.checkProjectCodeUnique(workProject))) {
            return AjaxResult.error("修改项目配置'" + workProject.getProjectCode() + "'失败，项目代码已存在");
        }
        workProject.setUpdateBy(getUsername());
        return toAjax(workProjectService.updateWorkProject(workProject));
    }

    /**
     * 删除项目配置
     */
    @PreAuthorize("@ss.hasPermi('work:project:remove')")
    @Log(title = "项目配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{projectIds}")
    public AjaxResult remove(@PathVariable Long[] projectIds) {
        return toAjax(workProjectService.deleteWorkProjectByIds(projectIds));
    }

    /**
     * 选择框列表
     */
    @GetMapping("/optionselect")
    public AjaxResult optionselect() {
        List<WorkProject> projects = workProjectService.selectProjectAll();
        return AjaxResult.success(projects);
    }

}
